package top.malaoshi.ctrl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.malaoshi.entity.Comment;
import top.malaoshi.vo.JsonResult;
import top.malaoshi.service.CommentSrvImpl;
import top.malaoshi.vo.JsonResultPage;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentCtrl {
    @Resource
    private CommentSrvImpl srv;
    @RequestMapping("queryByGoodsId")
    public JsonResult queryByGoodsId(String goodsId,String pageNum,String pageSize){
        JsonResultPage<List> res=new JsonResultPage<>();
        if(goodsId==null || "".equals(goodsId)){
            res.setCode(100);
            res.setMsg("请输入要查询的ID！");
            return res;
        }
        int pageNumInt=0;
        int pageSizeInt=10;
        if(pageNum!=null && !"".equals(pageNum)) {
            try {
                pageNumInt = Integer.parseInt(pageNum);
            }catch (Exception e){
                res.setCode(100);
                res.setMsg("请输入页数！");
                return res;
            }
        }
        if(pageSize!=null && !"".equals(pageSize)) {
            try {
                pageSizeInt=Integer.parseInt(pageSize);
            }catch (Exception e){
                res.setCode(100);
                res.setMsg("请输入每页显示记录数！");
                return res;
            }
        }
//        List<Comment> list=srv.queryByGoodsId(goodsId);
        PageInfo<Comment> page=srv.queryByGoodsId(goodsId,pageNumInt,pageSizeInt);
        res.setCode(0);
        res.setData(page.getList());
        res.setPageNum(pageNumInt);//当前页数
        res.setPageSize(pageSizeInt);//每页显示的记录数
        res.setPages(page.getPages());//总共的页数
        res.setTotal(page.getTotal());//总共记录数

        return res;
    }
}
